require 'test/unit'
require '../lib/cache'

class TestCache < Test::Unit::TestCase
  
  def setup
    @cache_file = "test_cache"
    @cache = Cache.new(@cache_file)
    @cache.dump
  end
  
  def test_new
    @cache = Cache.new(@cache_file)
    assert_equal(@cache.dictionary.class, Dictionary)
    assert_equal(@cache.documents.class, Hash)
    assert_equal(@cache.cache_file, @cache_file)
  end
  
  def test_add_document
    File.open("test_content") do |file|
      @cache.add_document(HTMLContent.new("test_content", file.mtime))
    end

    assert_equal(@cache.documents.size, 1)
    assert_equal(@cache.dictionary.size, 4)
  end
  
  def test_delete_document
    File.open("test_content") do |file|
      @cache.add_document(HTMLContent.new("test_content", file.mtime))
    end
    
    size = @cache.documents.size
    retValue = @cache.delete_document("aNotExistingDocument")
    assert_equal(@cache.documents.size, size)
    assert_equal(retValue, nil)
    
    
    size = @cache.documents.size
    @cache.delete_document("test_content")
    assert_equal(@cache.documents.size, size - 1)
  end
  
  def test_dump
    File.delete(@cache_file)
    @cache.dump
    assert_equal(File.exist?(@cache_file), true)
    
    File.open("test_content") do |file|
      @cache.add_document(HTMLContent.new("test_content", file.mtime))
    end
    size = @cache.documents.size
    @cache.dump
    
    @cache = Cache.new(@cache_file)
    assert_equal(size, @cache.documents.size)
  end
  
end
